(function(window,undefined){
	if(!window.BC){
		window['BC'] = {};
	}
	
	//生成预览图
	BC.createPreImg = function(obj,imgObj,size){
		window.URL = window.URL || window.webkitURL;
		var files = obj.files,
			img = imgObj;
		if(files[0].size>size*1024*1024){
			return {success:false,msg:'图片太大啦'};
		}else{
			if(window.URL){
				//File API
			      img.src = window.URL.createObjectURL(files[0]); //创建一个object URL，并不是你的本地路径
			      img.onload = function(e) {
			         window.URL.revokeObjectURL(this.src); //图片加载后，释放object URL
			      }
			}else if(window.FileReader){
				//opera不支持createObjectURL/revokeObjectURL方法。我们用FileReader对象来处理
				var reader = new FileReader();
				reader.readAsDataURL(files[0]);
				reader.onload = function(e){
					img.src = this.result;
				}
			}else{
				//ie
				obj.select();
				obj.blur();
				var nfile = document.selection.createRange().text;
				document.selection.empty();
				img.src = nfile;
			}
		}
		return {success:true,name:files[0].name,size:files[0].size}
	};
	//取得字符串的字节长度
	BC.getByteLen = function(str){
		var len = 0 ;
		for(var i=0; i<str.length; i++){
			if(str[i].match(/[^\x00-\xff]/ig) != null){ //全角
				len += 2;
			}else{
				len += 1;
			}
		}
		return len ;
	};
	//根据字节长度截取字符串
	BC.getByteVal = function(str,num){
		var len = 0 ,
			returnValue = '';
		for(var i=0; i<str.length; i++){
			if(str[i].match(/[^\x00-\xff]/ig) != null){ //全角
	            len += 2;
	        }else{
	            len += 1;
			}
			if(len>num){
				break;
			}
			returnValue += str[i];
		}
		return returnValue;
	};
	//操作cookie
	BC.CookieUtil = {
		get:function(name){
			var cookieName = encodeURIComponent(name)+'=',
				cookieStart = document.cookie.indexOf(cookieName),
				cookieValue = null;
			if(cookieStart>-1){
				var cookieEnd = document.cookie.indexOf(';',cookieStart);
				if(cookieEnd == -1){
					cookieEnd = document.cookie.length;
				}
				cookieValue = decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length, cookieEnd));
			}
			return cookieValue;
		},
		//expires传过期月数
		set:function(name,value,expires,path,domain,secure){
			var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);
			if(expires){
				var today = new Date();
				expires *= 2592000000 ;
				var expires_date = new Date(today.getTime()+expires) ;
				cookieText += '; expires=' + expires_date.toGMTString();
			}
			if(path){
				cookieText += '; path=' + path;
			}
			if(domain){
				cookieText += '; domain=' + domain;
			}
			if(secure){
				cookieText += '; secure'; 
			}
			document.cookie = cookieText;
		},
		unset:function(name,path,domain,secure){
			this.set(name,'',new Date(0),path,domain,secure);
		}
	};
	/*	返回顶部
		@param divId 目标div的ID
		@param time scrTop 滚动条滚动距离
		@param delay 延时时间
		@param acceleration 加速度
		@param time 滚动条动画时间间隔 (毫秒)
	*/
	BC.returnTop = function(divId,scrTop,delay,fn){
		var $target = $(divId),
			isDown = true,	//是否需要判断往下滚
			delay = delay || 20,
			scrTop = scrTop || 200,
			timer = null;
			$target.click(function(){
				$('html,body').animate({scrollTop:'0px'},300,'swing',fn);
			});
			$(document).scroll(function(){
				if(timer){
					window.clearTimeout(timer);
				}
				timer = window.setTimeout(function(){
					var scrollTop = $(document).scrollTop();
					if(isDown && scrollTop>200){
						$target.fadeIn();
						isDown = false;
					}else if(!isDown && scrollTop<=200){
						$target.fadeOut();
						isDown = true;
					}
				},delay);
			});
		/*
		//实现div渐变的函数
		function change(target,yOn){
			var target = target;
			var yOn = yOn ;
			yOn==1 ? alpha+=15 : alpha-=15 ;
			target.style.opacity = alpha/100;
			target.style.filter = "alpha(opacity = " + alpha + ")" ;
			if(alpha>0&&alpha<100){
				timer = setTimeout(function(){change(target,yOn)},20);
			}
			if(alpha<=0){
				target.style.display="none";
			}
		}
		*/
		//实现页面返回顶部的函数
		/*function goTop(acceleration, time) {
		 var acceleration = acceleration || 0.1;
		 var time = time || 16;
		 var x = $(document).scrollLeft() ;
		 var y = $(document).scrollTop() ;
		  滚动距离 = 目前距离 / 速度, 因为距离原来越小, 速度是大于 1 的数, 所以滚动距离会越来越小		 var speed = 1 + acceleration;
		 window.scrollTo(Math.floor(x / speed), Math.floor(y / speed));
		 
		  如果距离不为零, 继续调用迭代本函数		 if(x > 0 || y > 0) {
		  window.setTimeout(function(){goTop(acceleration,time)}, time);
		 }
		}*/
		
	};
	//邮箱校验
	BC.checkEmail = function(email){
		if(email){
			if(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z0-9]{2,4}$/.test(BC.trimAll(email))){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	};
	//用户名校验
	BC.checkUserName = function(val){
		if(val){
			if(/^\w{2,20}$/.test(val)){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	};
	//密码校验
	BC.checkPassword = function(psw){
		if(psw){
			if(psw.length>=6 && psw.length<=20){
				return true;
			}else{
				return false;
			}
		}else{
			return false
		}
	};
	//手机号校验
	BC.checkMobile = function(tel){
		if(tel){
			if(/(^((13[0-9])|(15[^4\D])|(18[0-9]))[0-9]{8}$)|(^[\d\+\-]{1,14}$)/.test(tel)){
				return true;
			}else{
				return false;
			}
		}else{
			return false;
		}
	};
	//检测验证码是否正确
	BC.checkRand = function(obj){
		this.ajax({
			type:'POST',
			data:{rand:obj.rand},
			url:'/bookcrossing/checkrand',
			dataType:'json',
			s:obj.s,
			e:obj.e,
			c:obj.c,
			scope:obj.scope
		});
	};
	//检测用户是否存在
	BC.checkUserExecute = function(obj){
		this.ajax({
			type:'POST',
			data:{checkkey:obj.uid},
			url:'/bookcrossing/checkuser',
			dataType:'json',
			b:obj.b,
			s:obj.s,
			f:obj.f,
			c:obj.c,
			scope:obj.scope
		});
	};
	//去除所有空格
	BC.trimAll = function(str){
		return str.replace(/\s/g,'');
	};
	//取得项目根路径
	BC.getRootPath = function(){
		var href = window.location.href ,	//完整路径
			pathName = window.location.pathname,	//主机后面的路径
			pos = href.indexOf(pathName),
			localhostPath = href.substring(0,pos),	//主机路径
			projectName = (/\/[^\/]+\//).exec(pathName)[0];
		return (localhostPath+projectName);
	};
	//ajax语法类似jQuery.ajax()
	BC.ajax = function(obj){
		var type = obj.type, url = obj.url, data = obj.data, dataType = obj.dataType || 'json', contentType = obj.ct, beforeSend = obj.b, success = obj.s,
			failed = obj.f, error = obj.e, complete = obj.c, async = obj.async, cache = obj.cache, timeout = obj.t,scope = obj.scope || window;
		jQuery.ajax({
			type:type,
			url:url,
			data:data,
			dataType:dataType,
			contentType:contentType,
			beforeSend:function(){
				if(beforeSend&&beforeSend.apply){
					beforeSend.apply(scope,arguments);	
				}
			},
			success:function(resp){
				if(dataType == 'json'){
					if(resp.success===true){
						success.apply(scope,arguments);
					}else if(failed&&failed.apply){
						failed.apply(scope,arguments);
					}
				}else{
					success.call(scope,resp);
				}
			},
			error:function(){
				if(error&&error.apply){
					error.apply(scope,arguments);
				}
			},
			complete:function(){
				if(complete&&complete.apply){
					complete.apply(scope,arguments);
				}
			},
			async:async,
			cache:cache,
			timeout:timeout
		});
	};
})(window);